package com.usian.behavior.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.behavior.mapper.ApBehaviorEntryMapper;
import com.usian.behavior.mapper.ApFollowBehaviorMapper;
import com.usian.behavior.service.ApFollowBehaviorService;
import com.usian.model.behavior.dtos.FollowBehaviorDto;
import com.usian.model.behavior.pojos.ApBehaviorEntry;
import com.usian.model.behavior.pojos.ApFollowBehavior;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class ApFollowBehaviorServiceImpl extends ServiceImpl<ApFollowBehaviorMapper, ApFollowBehavior>implements ApFollowBehaviorService {

    @Autowired
    private ApFollowBehaviorMapper apFollowBehaviorMapper;

    @Autowired
    private ApBehaviorEntryMapper apBehaviorEntryMapper;

    @Override
    public ResponseResult saveFollowBehavior(FollowBehaviorDto dto) {
        if (dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        ApBehaviorEntry apBehaviorEntry = apBehaviorEntryMapper.findByUserIdOrEquipmentId(dto.getUserId(), null);
        ApFollowBehavior alb=new ApFollowBehavior();
        if (apBehaviorEntry!=null){
            alb.setEntryId(apBehaviorEntry.getId());
        }
        alb.setCreatedTime(new Date());
        alb.setArticleId(dto.getArticleId());
        alb.setFollowId(dto.getFollowId());
        apFollowBehaviorMapper.saveFollowBehavior(alb);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
